﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace Windows_Photo_Viewer
{
    class DataAccesObject
    {
        private MySqlConnection con;
        private String connectionAuthData;
        private String serverThuis = "192.168.1.2";
        private String serverNietThuis = "server.wildewouter.nl";
        public String s;
        public DataAccesObject()
        {
            connectionAuthData = "Server="+serverThuis+";Uid=user;Pwd=passwd1;Database=images;";
            
            con = new MySqlConnection(connectionAuthData);

            try
            {
                con.Open();
                MessageBox.Show("Connection to database was succesfull", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            s = getImageFromDB(1);
        }

        private String getImageFromDB(int id)
        {
            String stmt = "SELECT image FROM image WHERE image_id = " + id;
            String imageBlob = "";
            try
            {
                MySqlCommand stment = new MySqlCommand(stmt, con);

                MySqlDataReader dr = stment.ExecuteReader();

                while (dr.Read())
                {
                    imageBlob = dr.GetString(dr.GetOrdinal("image"));
                }
                dr.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                /*"Something went wrong with extracting the image from the database"*/
            }

            return imageBlob;
        }
    }
}
